package com.itheima.Service.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.AdminPet;
import com.itheima.mapper.AdminPetMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Pet;
import com.itheima.pojo.PetQueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class AdminPetService implements AdminPet {
    @Autowired
    private AdminPetMapper adminPetMapper;

    @Override
    public PageBean page(PetQueryParam petQueryParam) {

        //设置分页参数
        PageHelper.startPage(petQueryParam.getPage(), petQueryParam.getPageSize());

        //执行查询
        Page<Pet> petList = adminPetMapper.list(petQueryParam);

        return new PageBean(petList.getTotal(), petList.getResult());
    }

    @Override
    public List<Pet> findAll(Integer id) {
        List<Pet> petList = adminPetMapper.findAll(id);
        return petList;
    }

    @Override
    public void deleteById(Integer id) {
        adminPetMapper.deleteById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addPet(Pet pet) {
        pet.setCreateTime(LocalDateTime.now());
        pet.setUpdateTime(LocalDateTime.now());
        adminPetMapper.addPet(pet);
    }

    @Override
    public Pet selectId(Integer id) {
        Pet pet = adminPetMapper.selectId(id);
        return pet;
    }

    @Override
    public void update(Pet pet) {
        pet.setUpdateTime(LocalDateTime.now());
        adminPetMapper.updateById(pet);
    }
}
